<template>
  <div>
    <el-dialog :title="dialogtitle" width="800px" class="icon-dialog" :visible.sync="show" :before-close="closeForm"
      append-to-body @open="open">
      <el-form ref="form" :size="size" :model="form" :rules="rules"
        :label-width="$store.getters.device !== 'mobile'?'20%':'90px'">
        <el-tabs v-model="activeName">
          <el-tab-pane style="padding-top:10px" label="基本信息" name="基本信息">
            <el-row>
              <el-col :span="24">
                <el-form-item label="任务标题" prop="title">
                  <el-input v-model="form.title" auto-complete="off" clearable placeholder="请输入名称" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">
                <el-form-item label="累计消费多少钱完成" prop="consumption">
                  <el-input v-model="form.consumption" style="width:300px;" auto-complete="off" clearable
                    placeholder="请输入累计消费多少钱完成">
                    <template slot="append">元</template>
                  </el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">
                <el-form-item label="直推多少人完成任务">
                  <el-input-number v-model="form.invitation" controls-position="right" style="width:300px;"
                    auto-complete="off" clearable :min="0" placeholder="请输入" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">
                <el-form-item label="直推多少人购买会员完成">
                  <el-input-number v-model="form.invitation_member" controls-position="right" style="width:300px;"
                    auto-complete="off" clearable :min="0" placeholder="请输入" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">
                <el-form-item label="完成任务条件">
                  <el-radio-group v-model="form.complete_where">
                    <el-radio :label="1">完成任意一项</el-radio><br>
                    <el-radio :label="2">全部完成</el-radio><br>
                  </el-radio-group>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">
                <el-form-item label="有效期">
                  <el-input-number v-model="form.expire_day" controls-position="right" style="width:160px;"
                    auto-complete="off" clearable :min="0" placeholder="" /> 月
                  <div class="help-block">0永久有效</div>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">
                <el-form-item label="多少小时后生效">
                  <el-input-number v-model="form.start_time" controls-position="right" style="width:160px;"
                    auto-complete="off" clearable :min="0" placeholder="" /> 小时
                  <div class="help-block">0为马上生效</div>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">
                <el-form-item label="完成任务送商品">
                  <div>
                    <span v-if="form.goods">
                      <el-image v-if="form.goods.image" class="goods_pic" :src="form.goods.image"
                        :preview-src-list="[form.goods.image]" />
                    </span>
                    <el-button size="mini" type="primary" plain @click="showSelectGoods = true">选择商品</el-button>
                  </div>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">
                <el-form-item label="完成任务赠送金额" prop="	balance">
                  <el-input v-model="form.balance" auto-complete="off" clearable placeholder="请输入升级价格" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">
                <el-form-item label="完成送优惠券">
                  <el-select v-model="form.coupon_id" style="width:100%" filterable clearable placeholder="请选择">
                    <el-option v-for="(item,i) in couponarray" :key="i" :label="item.key" :value="item.val" />
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">
                <el-form-item label="购买送优惠券数量">
                  <el-input-number v-model="form.number" controls-position="right" style="width:300px;"
                    auto-complete="off" clearable :min="0" placeholder="请输入" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">
                <el-form-item label="描述" prop="description">
                  <el-input v-model="form.description" type="textarea" auto-complete="off"
                    :autosize="{ minRows: 2, maxRows: 4}" clearable placeholder="请输入描述" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">
                <el-form-item label="状态" prop="status">
                  <el-switch v-model="form.status" :active-value="1" :inactive-value="0" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">
                <el-form-item label="排序">
                  <el-input-number v-model="form.sort" controls-position="right" style="width:200px;"
                    auto-complete="off" clearable :min="0" placeholder="排序" />
                </el-form-item>
              </el-col>
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="loading" type="primary" @click="submit">
          <span v-if="!loading">保 存</span>
          <span v-else>提 交 中...</span>
        </el-button>
        <el-button @click="closeForm">取 消</el-button>
      </div>
    </el-dialog>
    <!--添加商品弹窗-->
    <SelectGoods :show.sync="showSelectGoods" opentype="task" size="mini" />
  </div>
</template>
<script>
  import SelectGoods from '@/views/memberauthgroup/selectGoods.vue'
  export default {
    name: 'membertaskupdate',
    components: {
      SelectGoods
    },
    props: {
      show: {
        type: Boolean,
        default: false
      },
      opentype: {
        type: String,
        default: 'add'
      },
      size: {
        type: String,
        default: 'small'
      },
      info: {
        type: Object
      }
    },
    data() {
      return {
        dialogtitle: '',
        form: {
          title: '',
          status: 1
        },
        couponarray: [],
        loading: false,
        showSelectGoods: false,
        rules: {
          title: [{
            required: true,
            message: '名称不能为空',
            trigger: 'blur'
          }]
        },
        activeName: '基本信息'
      }
    },
    watch: {
      show(val) {
        if (val) {
          this.$api.post('/membertask/getField', {
            ptype: 1
          }).then(res => {
            this.couponarray = res.data.couponarray
          })
        }
      }
    },
    methods: {
      open() {
        this.form = this.info
        if (this.opentype == 'update') {
          this.dialogtitle = '修改'
        } else {
          this.dialogtitle = '添加'
        }
      },
      submit() {
        this.$refs['form'].validate(valid => {
          if (valid) {
            this.loading = true
            this.$api.post('/membertask/update', this.form).then(res => {
              this.$message({
                message: res.msg,
                type: 'success'
              })
              this.$emit('refesh_list')
              this.closeForm()
            }).catch(() => {
              this.loading = false
            })
          }
        })
      },
      closeForm() {
        this.$emit('update:show', false)
        this.loading = false
        if (this.$refs['form'] !== undefined) {
          this.$refs['form'].resetFields()
        }
      }
    }
  }
</script>
<style scoped>
  .goods_pic {
    width: 100px;
    vertical-align: middle;
  }
</style>
